
var WechatAPI = require('wechat-api');
var api = new WechatAPI('wxb0eccbcc17a376a0', 'ce450f11ba4a604ed74f49bc9caed8bf');
var express = require('express');
var router = express.Router();


/* GET wechat img list api. */
router.get('/getMaterials',function (req, res, next) {
  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  var type=req.param('type');//根据type返回不同类型的list
  api.getMaterials(type,0,20,
        function(_err, _result, _res) {
         	if(_err!=null){
				res.send(_err);
				console.log(_err);
				return;
			 }
			 if(_res!=null){
				 console.log(_res);
			 }
			 if(_result!=null){
				 res.send(_result);
				 console.log(_result);
			 }
        })
});
/* GET wechat material detail api. */
router.get('/getMaterial',function (req, res, next) {
  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  var media_id=req.param('media_id');//根据media_id获取资源
  api.getMaterial(media_id,
        function(_err, _result, _res) {
         	if(_err!=null){
				res.send(_err);
				console.log(_err);
				return;
			 }
			 if(_res!=null){
				 console.log(_res);
			 }
			 if(_result!=null){
				 res.send(_result);
				 console.log(_result);
			 }
        })
});
/* delete wechat material api. */
router.get('/removeMaterial', function (req, res, next) {
  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  var media_id=req.param('media_id');
  api.removeMaterial(media_id,
        function(_err, _result, _res) {
         	if(_err!=null){
				res.send(_err);
				console.log(_err);
				return;
			 }
			 if(_res!=null){
				 console.log(_res);
			 }
			 if(_result!=null){
				 res.send(_result);
				 console.log(_result);
			 }
        })
});
/* upload wechat material api */
router.get('/uploadMaterial', function (req, res, next) {
  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  var file_path=req.param('filepath');
  var _type = req.param('type');
  api.uploadMaterial(file_path,_type,
        function(_err, _result) {
         	if(_err!=null){
				res.send(_err);
				console.log(_err);
				return;
			 }
			 if(_result!=null){
				 res.send(_result);
				 console.log(_result);
			 }
        })
});
/* upload wechat message api */
router.post('/uploadNewsMaterial', function (req, res, next) {
  res.setHeader('Content-Type', 'application/json;charset=utf-8');

  var _news=req.body;
  var str_news=JSON.stringify(_news);
  api.uploadNewsMaterial(str_news,
        function(_err, _result) {
         	if(_err!=null){
				res.send(_err);
				console.log(_err);
				return;
			 }
			 if(_result!=null){
				 res.send(_result);
				 console.log(_result);
			 }
        })
});
module.exports = router;
